 // pages/admin/my/purchase/index.js
const app=getApp()
Page({
  data: {
    bill: '',
    goodslist: '',
    order: '',
    list: '',
    detail: '',
    show: false,
    showdetail: false,
    minDate: new Date(2019, 8, 1).getTime(),
    maxDate: new Date().getTime(),
    currentDate: '',
    formatter(type, value) {
      if (type === 'year') {
        return `${value}年`;
      } else if (type === 'month') {
        return `${value}月`;
      }
      return value;
    },
  },
  formatGoods(list){
    var goodslist = []
    list.forEach(item => {
      goodslist.push(...item.goodslist)
    })
    var goodsTotal = [];  // 存最终数据结果

    // 数据按照水果名称进行归类
    var nameContainer = {}; // 针对键name进行归类的容器
    goodslist.forEach(item => {
      nameContainer[item.goods] = nameContainer[item.goods] || [];
      nameContainer[item.goods].push(item);
    });

    //console.log(nameContainer); // 按照水果名称归类完成：{ apple: Array(2), banana: Array(2) }

    // 统计不同种类水果的数量
    var fruitName = Object.keys(nameContainer); // 获取水果种类：["apple", "banana"]
    fruitName.forEach(nameItem => {
      let count = 0;
      nameContainer[nameItem].forEach(item => {
        count += item.value; // 遍历每种水果中包含的条目计算总数
      });
      goodsTotal.push({ 'goods': nameItem, 'value': count });
    });
    return goodsTotal
  },
  formatService(service){
    var list = []  //最后结果
    var localContainer = {}  //对地址进行归类
    service.forEach(item => {
      localContainer[item.local] = localContainer[item.local] || [];
      localContainer[item.local].push(item);
    })
    var name = Object.keys(localContainer)
    name.forEach(localitem=>{
      list.push({'local': localitem, 'value': localContainer[localitem]})
    })
    return list
  },
  format(date) {
    const time = new Date(date)
    const year = time.getFullYear()
    const month = time.getMonth() + 1
    const day = time.getDate()

    return [year, month, day].map(this.formatNumber).join('-')
  },
  formatNumber(n){
    n = n.toString()
    return n[1] ? n : '0' + n
  },
  // 确定选择日期弹窗
  onConfirm(e) {
    if (!isNaN(e.detail)){
      var t = this.format(e.detail)
    }else{
      var t = this.format(new Date())
    }
    this.setData({
      currentDate: t,
      show: false
    })
    app.api.getData('admin/bill/time', { time: this.data.currentDate }, res => {
      var that = this
      this.setData({
        goodslist: that.formatGoods(res.data)
      })
    })
    app.api.getData('admin/order/bill', { time: this.data.currentDate }, res => {
      var a = []
      res.data.forEach(item => {
        let data = { ...item.address }
        data = Object.assign(data[0], { id: item._id })
        a.push(data)
      })
      res.data.forEach(item => {
        item.order_time = app.util.formatTime(item.order_time)
      })
      var list = this.formatService(a)
      this.setData({
        order: res.data,
        list: list,
      })
    })
  },
  // 关闭选择日期弹窗
  onCancel() {
    this.setData({
      show: false
    })
  },
  showpopup: function () {
    if (!this.show) {
      this.setData({
        show: true
      })
    } else {
      this.setData({
        show: false
      })
    }
  },
  showDetail(e){
    if (!this.showdetail) {
      this.setData({
        showdetail: true
      })
    } else {
      this.setData({
        showdetail: false
      })
    }
    var detail = this.data.order.filter(item=>{
         return item._id == e.currentTarget.dataset.id
    })
  this.setData({
    detail:detail
})
  },
  close(){
      this.setData({
        showdetail: false
      })
  },
  onLoad: function (options) {
    var t = this.format(new Date())
    this.setData({
      currentDate: t
    })
    app.api.getData('admin/bill/time', { time: this.data.currentDate }, res => {
      var that = this
      this.setData({
        goodslist: that.formatGoods(res.data)
      })
    })
    app.api.getData('admin/order/bill', { time: this.data.currentDate }, res => {
      var a = []
      res.data.forEach(item=>{
        let data = {...item.address}
            data = Object.assign(data[0],{id: item._id})
        a.push(data)
      })
      res.data.forEach(item => {
        item.order_time = app.util.formatTime(item.order_time)
      })
     var list = this.formatService(a)
      this.setData({
        order: res.data,
        list: list
      })
    })
  },
  onShow () {
  
  }
})